﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using CMN.Controllers;
using CMN.Controllers.Filters;
using CMN.Common;
using System.Text;
using CMN.Framework;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace CMN.Controllers
{
    [UserAuthorize]
    [LoggerFilter()]
    [ExceptionFilter()]
    public class HomeController : Controller
    {
       
        public ActionResult Index()
        {
            if (!System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
            {
                Response.Redirect("~/manager/user/index");
                return null;
            }
            return View("Index");//页面地址
        }
              
        public string TreeList()
        {
            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = int.MaxValue;
            qp.Where = "m_close=0";
            int recordNum = 0;
            IDataService<Sys_ModuleEntity> Sys_ModuleSv = ServiceBuilder.BuildSys_ModuleService();
            List<Sys_ModuleEntity> sys_moduleList = Sys_ModuleSv.List(qp, out recordNum);
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            GetTreeString(sb, 0, sys_moduleList);
            sb.Append("]");
            string reStr = sb.ToString();
            if (reStr.EndsWith(","))
            {
               reStr= reStr.Remove((reStr.Length - 1), 1);
            }
            return reStr; 
        }

        public string GetUserInfo()
        {
            IsoDateTimeConverter jsDataConvert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            jsDataConvert.DateTimeFormat = "yyyy-MM-dd";
            Sys_UserEntity userInfo = UserData.GetUserData;
            string returnJson = string.Empty;
            if (userInfo != null)
            {
                returnJson = string.Concat("{success:true,data:", JsonConvert.SerializeObject(userInfo, Formatting.Indented, jsDataConvert), "}");
            }
            else
            {
                returnJson = string.Concat("{success:false,data:[]}");
            }
            return returnJson;
        }
        /// <summary>
        /// 获取树状json 列表
        /// </summary>
        /// <param name="sb">记录容器</param>
        /// <param name="id">起始id</param>
        /// <param name="list">要循环的列表</param>
        protected void GetTreeString(StringBuilder sb, int id, List<Sys_ModuleEntity> list)
        {

            bool isHasChild = false;

            int intCount = 0;
            int intIndex = 0;
            foreach (Sys_ModuleEntity module in list)
            {
                if (module.M_ParentID == id)
                {
                    if ((module.M_ParentID == 0 || UserData.CheckPageCode(Utils.GetUserID, module.M_PageCode)) && module.M_Close != 1)
                        intCount++;
                }
            }

            if (intCount != 0)
            {
                foreach (Sys_ModuleEntity module in list)
                {


                    if (module.M_ParentID == id && module.M_Close == 0)
                    {
                        if (module.M_ParentID == 0 || UserData.CheckPageCode(Utils.GetUserID, module.M_PageCode))
                        {
                            intIndex++;
                            foreach (Sys_ModuleEntity module2 in list)
                            {
                                if ((module2.M_ParentID == module.ModuleID) && UserData.CheckPageCode(Utils.GetUserID, module2.M_PageCode))
                                {
                                    isHasChild = true;
                                    break;
                                }
                            }
                            if (isHasChild || (id != 0))
                            {
                                sb.Append(string.Concat("{text:'", module.M_CName, "',id:'", module.ModuleID, "',url:'", module.M_Directory, "'"));

                                foreach (Sys_ModuleEntity module2 in list)
                                {
                                    if (module2.M_ParentID == module.ModuleID)
                                    {
                                        isHasChild = true;
                                        sb.Append(",expanded:false,children:[");
                                        break;
                                    }
                                }

                                GetTreeString(sb, module.ModuleID, list);

                                if (isHasChild)
                                {
                                    if (intIndex < intCount)
                                    {

                                        sb.Append("]},");
                                    }
                                    else
                                    {
                                        sb.Append("]}");
                                    }

                                }
                                else
                                {
                                    if (intIndex < intCount)
                                    {
                                        sb.Append(",leaf:true},");
                                    }
                                    else
                                    {
                                        sb.Append(",leaf:true}");
                                    }
                                }
                            }
                            isHasChild = false;
                        }
                    }

                }
            }

        }
    }
}
